//
//  MasterDetailController.swift
//  BodyBuilding
//
//  Created by franze on 2017/10/20.
//  Copyright © 2017年 franze. All rights reserved.
//

import UIKit

class MasterDetailController: UIViewController {
    var infoDict:NSDictionary!

    override func viewDidLoad() {
        super.viewDidLoad()

        viewConfig()
    }
    
    func viewConfig(){
        view.backgroundColor = UIColor.white
        
        let navBar = CustomNavBar(frame: CGRect(origin: .zero, size: CGSize(width: WIDTH, height: 64)))
        navBar.initialize(text: infoDict.value(forKey: "realname") as? String)
        view.addSubview(navBar)

        let imageView = UIImageView()
        imageView.sd_setImage(with: URL(string: BB_IMAGEURLPRE+(infoDict.value(forKey: "thumb") as! String)), placeholderImage: UIImage(named:"placeholder"))
        imageView.frame = CGRect(x: 0, y: 64, width: WIDTH, height: (HEIGHT-69)/3)
        view.addSubview(imageView)
        
        let lessonLabel = UILabel(frame: CGRect(x: 100, y: 90+imageView.bounds.height, width: 270, height: 20))
        lessonLabel.font = UIFont.systemFont(ofSize: 20)
        lessonLabel.center.x = WIDTH/2
        lessonLabel.text = "教授课程: \(infoDict.value(forKey: "actcate_name") as! String)"
        view.addSubview(lessonLabel)
        
        let clubLabel = UILabel(frame: CGRect(x: 100, y: 40+lessonLabel.frame.origin.y, width: 270, height: 20))
        clubLabel.font = UIFont.systemFont(ofSize: 20)
        clubLabel.center.x = WIDTH/2
        clubLabel.text = "所在俱乐部: 帝壹射箭俱乐部"
        view.addSubview(clubLabel)
        
        let timeLabel = UILabel(frame: CGRect(x: 100, y: 40+clubLabel.frame.origin.y, width: 270, height: 20))
        timeLabel.font = UIFont.systemFont(ofSize: 20)
        timeLabel.center.x = WIDTH/2
        timeLabel.text = "授课时间: 14:00-20:00"
        view.addSubview(timeLabel)
        
        let countLabel = UILabel(frame: CGRect(x: 100, y: 40+timeLabel.frame.origin.y, width: 270, height: 20))
        countLabel.font = UIFont.systemFont(ofSize: 20)
        countLabel.center.x = WIDTH/2
        countLabel.text = "关注人数: \(infoDict.value(forKey: "hits") as! String)人"
        view.addSubview(countLabel)
        
        let focusBtn = CustomButton(frame: CGRect(x: 0, y: countLabel.frame.origin.y+100, width: 100, height: 44))
        focusBtn.backgroundColor = UIColor.white
        focusBtn.circleColor = UIColor.black
        focusBtn.center.x = WIDTH/2-100
        focusBtn.setTitle("关注", for: .normal)
        focusBtn.setTitleColor(.black, for: .normal)
        focusBtn.layer.borderWidth = 1
        focusBtn.layer.borderColor = UIColor.lightGray.cgColor
        focusBtn.layer.cornerRadius = 8
        focusBtn.adjustsImageWhenDisabled = false
        focusBtn.isEnabled = false
        focusBtn.animation()
        view.addSubview(focusBtn)
        
        let askBtn = UIButton(frame: CGRect(x: 0, y: countLabel.frame.origin.y+100, width: 100, height: 44))
        askBtn.backgroundColor = UIColor.white
        askBtn.center.x = WIDTH/2+100
        askBtn.setTitle("提问", for: .normal)
        askBtn.setTitleColor(.black, for: .normal)
        askBtn.layer.borderColor = UIColor.lightGray.cgColor
        askBtn.layer.borderWidth = 1
        askBtn.layer.cornerRadius = 8
        view.addSubview(askBtn)
        
        let joinBtn = UIButton(frame: CGRect(x: 0, y: HEIGHT-74, width: 150, height: 44))
        joinBtn.backgroundColor = UIColor.red
        joinBtn.center.x = WIDTH/2
        joinBtn.layer.cornerRadius = 8
        if UserDefaults.standard.value(forKey: infoDict.value(forKey: "id") as! String) == nil {
            joinBtn.setTitle("报名", for: .normal)
        }else{
            joinBtn.setTitle(UserDefaults.standard.value(forKey: infoDict.value(forKey: "id") as! String) as? String, for: .normal)
        }
        view.addSubview(joinBtn)
        
        isExits(button: focusBtn)
        
        focusBtn.addTarget(self, action: #selector(focus), for: .touchUpInside)
        askBtn.addTarget(self, action: #selector(ask), for: .touchUpInside)
        joinBtn.addTarget(self, action: #selector(join), for: .touchUpInside)
        navBar.backBtn.addTarget(self, action: #selector(back), for: .touchUpInside)
    }
    
    func isExits(button:CustomButton){
        let parameters:NSDictionary = [
            "SessionID"     :   LoginModel.sessionID,
            "fan_id"        :   infoDict.value(forKey: "id") as! String,
            "userid"        :   LoginModel.userID,
            "fans_type"     :   "2",
            ]
        Networking.shared.isExits(parameter: parameters) {(success) in
            DispatchQueue.main.async {
                if success{
                    button.setTitle("取消关注", for: .normal)
                }
                button.isEnabled = true
                button.stopAnimation()
            }
        }
    }
    
    @objc func join(sender:UIButton){
        if sender.titleLabel?.text == "报名"{
            let alert = UIAlertController(title: "确认报名", message: nil, preferredStyle: .alert)
            let confirmAction = UIAlertAction(title: "确认", style: .default) { (_) in
                UserDefaults.standard.setValue("已报名", forKey: self.infoDict.value(forKey: "id") as! String)
                sender.setTitle(UserDefaults.standard.value(forKey: self.infoDict.value(forKey: "id") as! String) as? String, for: .normal)
            }
            let cancelAction = UIAlertAction(title: "取消", style: .default, handler: nil)
            alert.addAction(confirmAction)
            alert.addAction(cancelAction)
            present(alert, animated: true, completion: nil)
        }else{
            let popup = Popup()
            popup.appear(title: "已报名", action: nil)
        }
    }
    
    @objc func focus(sender:UIButton){
        let parameter:NSDictionary = [
            "SessionID" :   LoginModel.sessionID,
            "fan_id"    :   self.infoDict.value(forKey: "id") as! String,
            "fans_type" :   "2",
            "userid"    :   LoginModel.userID
        ]
        if sender.isEnabled{
            let popup = Popup()
            if sender.titleLabel?.text == "关注"{
                Networking.shared.focus_Join(parameter: parameter) {
                    DispatchQueue.main.async {
                        popup.appear(title: "关注成功", action: nil)
                        sender.setTitle("取消关注", for: .normal)
                    }
                }
            }else{
                Networking.shared.delete(parameter: parameter, block: {
                    DispatchQueue.main.async {
                        popup.appear(title: "取消关注成功", action: nil)
                        sender.setTitle("关注", for: .normal)
                    }
                })
            }
        }
    }
    
    @objc func ask(){
        let controller = TeacherAskController()
        controller.masterID = infoDict.value(forKey: "id") as! String
        
        hidesBottomBarWhenPushed = true
        _ = navigationController?.pushViewController(controller, animated: true)
    }
    
    @objc func back(){
        _ = navigationController?.popViewController(animated: true)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}
